package com.tutor.lzm.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.TeacherDetailsAct;
import com.tutor.lzm.adapter.BecomeTutorAdapter;
import com.tutor.lzm.adapter.SingleCourseAdapter;
import com.tutor.lzm.adapter.TeacherAdapter;
import com.tutor.lzm.bean.SubjectBean;
import com.tutor.lzm.bean.TeacherRecommendBean;
import com.tutor.lzm.http.request.GetAllSubjectRequest;
import com.tutor.lzm.http.response.TutorRequiresSearchBean;
import com.tutor.lzm.function.tutor.TutorDetailAct;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TutorRequiresRequest;
import com.tutor.lzm.http.response.TutorRequiresSearchList;
import com.tutor.syslib.Logs;
import com.tutor.syslib.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzgpc on 2017-8-24.
 * 做家教
 */
public class BecomeTutorFragment extends BaseFragment {
    @BindView(R.id.popular_recommendation_listView)
    MyListView myListView;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    Unbinder unbinder;
    /**
     * 加载位置坐标
     */
    private int currentPage = 0;
    List<TutorRequiresSearchBean> tutorRequiresList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getFragmentLayout() {
        return R.layout.fragment_become_tutor;
    }

    @Override
    protected void initView(Bundle bundle) {
        super.initView(bundle);
        initTutorListView();

//        initData();
    }

    /**
     * 加载热门老师listView
     */
    private void initTutorListView() {
        BecomeTutorAdapter adapter = new BecomeTutorAdapter(getContext(), tutorRequiresList);
        myListView.setAdapter(adapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TutorDetailAct.start(getContext(), tutorRequiresList.get(position).searchId);
            }
        });

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {// 不在最前端界面显示
        } else {// 重新显示到最前端中
            List<SubjectBean> subjectList = AppSession.getInstance().getSubjectList();
            if (subjectList == null) {
                getAllSubject();
                return;
            }
            initSubject(subjectList);
        }
    }

    /**
     * 获取所有学科
     */
    private void getAllSubject() {
        GetAllSubjectRequest request = new GetAllSubjectRequest();
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                try {
                    List<SubjectBean> subjectList = (List<SubjectBean>) response;
                    AppSession.getInstance().setSubjectList(subjectList);
                    initSubject(subjectList);
                    Logs.e(tag, "subjectList大小为 " + subjectList.size());
                } catch (Exception e) {
                    Logs.e(tag, "onSuccess   getAllSubject接口错误， e=" + e);
                }

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.e(tag, "onFailListener    getAllSubject接口错误， msg=" + msg);
            }
        });
    }

    /**
     * 初始化学科
     */
    private void initSubject(List<SubjectBean> subjectList) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        SingleCourseAdapter singleCourseAdapter = new SingleCourseAdapter(getContext(), subjectList);
        recyclerView.setAdapter(singleCourseAdapter);

    }


//    private void initData() {
//
//        getAllRequiresSearch();
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view

        contentView = inflater.inflate(getFragmentLayout(), container, false);
        unbinder = ButterKnife.bind(this, contentView);
        super.onCreateView(inflater, container, savedInstanceState);
        return contentView;
    }


    @Override
    protected void initData(Bundle bundle) {
        super.initData(bundle);
        getAllRequiresSearch();
    }

    /**
     * 获取所有家教请求
     */
    private void getAllRequiresSearch() {
        final TutorRequiresRequest request = new TutorRequiresRequest();
        request.setLimit(10);
        request.setStart(currentPage);
        request.setTeacherId(AppSession.getInstance().getUserId());
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                try {
                    TutorRequiresSearchList tutorRequiresSearchList = (TutorRequiresSearchList) response;
                    List<TutorRequiresSearchBean> list = tutorRequiresSearchList.publishSearchList;
                    if (list != null && list.size() > 0) {
                        tutorRequiresList.addAll(list);
                        Logs.e(tag, "tutorRequiresList大小为 " + tutorRequiresList.size());
                    } else {
                        Logs.e(tag, "tutorRequiresList为空 ");

                    }
                    if (list.size() != 0) {
                        currentPage++;
                    }
                } catch (Exception e) {
                    Logs.e(tag, "onSuccess   tutorRequiresList接口错误， e=" + e);
                }

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.e(tag, "onFailListener   tutorRequiresList接口错误， msg=" + msg);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //解绑ButterKnife
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
